// pages/collect/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        name: '商品收藏',
        isActive: true
      },
      {
        id: 1,
        name: '品牌收藏',
        isActive: false
      },
      {
        id: 2,
        name: '店铺收藏',
        isActive: false
      },
      {
        id: 3,
        name: '浏览足迹',
        isActive: false
      }
    ],
    //收藏商品数据
    collects: [],
    //浏览足迹
    history: []
  },
  //tab栏的切换
  changeTab(e) {
    console.log(e)
    const {
      index
    } = e.detail
    //深拷贝tabs
    let tabs = JSON.parse(JSON.stringify(this.data.tabs))
    //遍历数组
    tabs.forEach((item, i) => i === index ? item.isActive = true : item.isActive = false)
    this.setData({
      tabs
    })
  },
  onShow() {
    //获取缓存中的收藏商品数据
    const collects = wx.getStorageSync('collects') || [];
    const history = wx.getStorageSync('history') || [];
    this.setData({
      collects,
      history
    })

    //获取地址栏中的参数，在onShow中不能直接拿
    //判断是否有token
    const token = wx.getStorageSync('token');
    if (!token) {
      //没有token，跳转授权页面
      wx.navigateTo({
        url: '/pages/auth/index'
      });
      return;
    }

    //onShow中获取url中参数的方法
    //1.获取当前小程序中的页面栈--数组，长度最大是10个页面
    let pages = getCurrentPages();
    //2.数组中最大索引的页面就是当前的页面
    let currentPage = pages[pages.length - 1]
    //获取url上的参数
    const type = currentPage.options.type
    //获取data中的tabs
    let {
      tabs
    } = this.data
    tabs.forEach((v, i) => i === parseInt(type) ? v.isActive = true : v.isActive = false)
    //设回data
    this.setData({
      tabs
    })
  },
  //下拉刷新触发事件
  onPullDownRefresh() {
    wx.showLoading({
      title: '加载中',
      duration: 1000,
      mask: true
    });
    const collects = wx.getStorageSync('collects');
    //1.重置数组
    this.setData({
      collects: []
    })
    //3.重新发起商品获取
    setTimeout(() => {
      wx.stopPullDownRefresh() //停止下拉刷新
      wx.hideLoading();
      this.setData({
        collects
      })
    }, 1000)
  },
  //点击删除触发
  deleteHistory(e) {
    const goods_id = e.currentTarget.dataset.gid
    //获取缓存中浏览数据
    const history = wx.getStorageSync('history') || [];
    const index = history.findIndex(v => v.goods_id === goods_id)
    console.log(index)
    if (index !== -1) {
      wx.showModal({
        title: '是否删除该条数据？',
        content: '',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            history.splice(index, 1)
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1500,
              mask: false
            });
            //保存回缓存
            wx.setStorageSync('history', history);
            this.setData({
              history
            })
          }
        }
      });
    }
  },
  deleteCollects(e) {
    const goods_id = e.currentTarget.dataset.gid
    //获取缓存中浏览数据
    const collects = wx.getStorageSync('collects') || [];
    const index = collects.findIndex(v => v.goods_id === goods_id)
    console.log(index)
    if (index !== -1) {
      wx.showModal({
        title: '是否删除该条数据？',
        content: '',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {
            collects.splice(index, 1)
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1500,
              mask: false
            });
            //保存回缓存
            wx.setStorageSync('collects', collects);
            this.setData({
              collects
            })
          }
        }
      });
    }


  },

})